package priv.winnine.handler;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import priv.winnine.entity.Result;
import priv.winnine.entity.enums.ResultEnum;
import priv.winnine.exception.ResultException;
import priv.winnine.util.ResultUtil;

import javax.servlet.http.HttpServletRequest;

@ControllerAdvice
@Slf4j
public class CommonExceptionHandler {

    @ExceptionHandler(value = ResultException.class)
    @ResponseBody
    public Result<Object> ResultExceptionHandle(Exception e) {
        ResultException exception = (ResultException) e;
        return ResultUtil.result(exception.getCode(), exception.getMessage(), exception.getData());
    }

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public <T> Result<T> Handle(Exception e, HttpServletRequest request) {
        log.error("接口【{}】的{}方法发生异常", request.getServletPath(), request.getMethod().toLowerCase());
        log.error(e.getMessage(), e);
        return ResultUtil.result(ResultEnum.FAIL);
    }

}
